﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class CouponAddViewModel : NotificationObject
    {
        private CouponAdd gWindow;
        private CouponViewModel gCouponViewModel;

        public CouponAddViewModel(CouponAdd pWindow, CouponViewModel pCouponViewModel)
        {
            gWindow = pWindow;
            gCouponViewModel = pCouponViewModel;

            Initialize();
        }

        public void Initialize()
        {
            Status.Add("停用");
            Status.Add("启用");
            StatusSelect = "停用";
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private float total;
        public float Total
        {
            get
            {
                return total;
            }

            set
            {
                total = value;
                RaisePropertyChanged("Total");
            }
        }

        private float discount;
        public float Discount
        {
            get
            {
                return discount;
            }

            set
            {
                discount = value;
                RaisePropertyChanged("Discount");
            }
        }

        private List<String> status = new List<string>();
        public List<String> Status
        {
            get
            {
                return status;
            }

            set
            {
                status = value;
                RaisePropertyChanged("Status");
            }
        }

        private String statusSelect;
        public String StatusSelect
        {
            get
            {
                return statusSelect;
            }

            set
            {
                statusSelect = value;
                RaisePropertyChanged("StatusSelect");
            }
        }

        private String endTime;
        public String EndTime
        {
            get
            {
                return endTime;
            }

            set
            {
                endTime = value;
                RaisePropertyChanged("EndTime");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            int status = 0;
            if(StatusSelect == "停用")
            {
                status = 0;
            }
            else if (StatusSelect == "启用")
            {
                status = 1;
            }

            String Time = Convert.ToDateTime(EndTime).ToString("yyyy/MM/dd");
            API.InsertCoupon(new CCoupon(Name, Total, Discount, status, Time));
            gCouponViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }
    }
}
